package com.gome.wechat.program.controller;

import java.util.Date;
import java.util.List;

import com.gome.wechat.program.service.queryservice.GroupActivityQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.gome.wechat.program.dto.GroupActivityDetailDto;
import com.gome.wechat.program.dto.GroupActivityDto;
import com.gome.wechat.program.dto.GroupActivityJoinDto;
import com.gome.wechat.program.dto.GroupActivityListDto;
import com.gome.wechat.program.dto.StringResponseDto;
import com.gome.wechat.program.entity.GroupActivity;
import com.gome.wechat.program.service.applicationservice.GroupActivityApplicationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by liwei113 on 2017/10/31 0031.
 */
@Api("组团活动服务")
@RestController
@RequestMapping(value = "/group/activity", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class GroupActivityController {


    @Autowired
    GroupActivityApplicationService groupActivityApplicationService;

    @Autowired
    GroupActivityQueryService groupActivityQueryService;

    @ApiOperation(value = "发布活动",response = StringResponseDto.class, notes = "开发by李巍")
    @PostMapping("/save")
    public StringResponseDto save(@RequestBody GroupActivityDto groupActivityDto) {
        if(groupActivityDto.getId()==null){
            groupActivityDto.setCreatedTime(new Date());
        }
        groupActivityDto.setType(1);
        groupActivityApplicationService.save(groupActivityDto);
        return new StringResponseDto("发布成功");
    }

    @ApiOperation(value = "参加活动",response = StringResponseDto.class, notes = "开发by李巍")
    @PostMapping("/join")
    public StringResponseDto join(@RequestBody GroupActivityJoinDto groupActivityJoinDto) {
        boolean join =  groupActivityApplicationService.checkJoin(groupActivityJoinDto);
        if(join){
            return new StringResponseDto("已经参加");
        }
        Long joinId = groupActivityApplicationService.joinGroupActivity(groupActivityJoinDto);
        return new StringResponseDto("参加成功");
    }

    @ApiOperation(value = "活动活动详情",response = GroupActivityDetailDto.class, notes = "开发by李巍")
    @GetMapping("/detail")
    public GroupActivityDetailDto detail(@RequestParam  Long  id) {
        GroupActivityDetailDto groupActivityDetailDto = groupActivityQueryService.getDetail(id);
        return groupActivityDetailDto;
    }

    @ApiOperation(value = "获取我发布的活动列表",response = GroupActivityListDto.class, notes = "开发by李巍")
    @GetMapping("/my/publish/list")
    public List<GroupActivity> myPublishGroupActivityList(@RequestParam Long  userId) {
        //GroupActivityService

        return groupActivityApplicationService.getGroupActivityByPublisherId(userId);
    }

    @ApiOperation(value = "获取我参加的活动列表",response = GroupActivityListDto.class, notes = "开发by李巍")
    @GetMapping("/my/join/list")
    public List<GroupActivity> myJoinGroupActivityList(@RequestParam  Long  userId) {
        //GroupActivityService
        return groupActivityApplicationService.getListByUserId(userId);
    }


    @ApiOperation(value = "获取某个点附件的活动",response = GroupActivityListDto.class, notes = "开发by李巍")
    @GetMapping("/nearby/list/{longitude}/{latitude}/{space}")
    public List<GroupActivity> nearbyList(@PathVariable("longitude")  Double longitude, @PathVariable("latitude")  Double latitude, @PathVariable("space")  Integer  space) {
        //GroupActivityService
    	

        Double spaceitude = Double.parseDouble(space/111000+"."+space%111000);
    	
        return groupActivityApplicationService.getGroupActivityByCoordinate((longitude*100)/100,(latitude*100)/100, (spaceitude*100)/100);
    }


    @ApiOperation(value = "轮训获取门店类型的活动最新的一条",response = GroupActivityListDto.class, notes = "开发by李巍")
    @GetMapping("/circulate/new/groupActivity")
    public List<GroupActivity> getGroupActivityByTypeTwoAndLimitOne() {
        //GroupActivityService
        return groupActivityApplicationService.getGroupActivityByTypeTwoAndLimitOne();
    }
}
